﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WinFormsAppTest.pojo;

namespace WinFormsAppTest.Dao
{
    internal class SignsDao
    {
        //使用hyl版本进行登录
        //String connectionString = @"Data Source=DESKTOP-SQERB5L\MSSQLSERVERR;" +
        //    "Initial Catalog=hylpersonsystem;" +
        //    "User ID=hyl;" +
        //    "Password=123456;";

        //使用window登录
        String connectionString = @"Data Source=DESKTOP-SQERB5L\MSSQLSERVERR;" +
            "Initial Catalog=hylpersonsystem;" +
            "Integrated Security=true;";



        //connect 无参
        public SqlConnection connect()
        {

            using (SqlConnection sc = new SqlConnection(connectionString))
            {  ///
                sc.Open();
                try
                {
                    //sc.Open();

                    // 检查连接状态
                    if (sc.State == ConnectionState.Open)
                    {
                        Console.WriteLine("数据库连接成功！");
                        MessageBox.Show("数据库连接成功！");
                        sc.Close();

                        // 连接成功后可以执行其他操作
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine("数据库连接失败：" + ex.Message);
                    MessageBox.Show("数据库连接失败！");
                    sc.Close();
                }
                return sc;
            }///
        }
        //


        //添加签到信息
        public void CreateSigns(DateTime begintime, DateTime endtime,
                                  int workid, int userid)
        {
            // 在这里执行具体的数据库操作
            // 例如使用 SqlCommand 执行查询语句或执行存储过程
            SqlConnection sc = new SqlConnection(connectionString);
            sc.Open();
            String sql = "insert into signs values(@Begintime,@Endtime,@Workid,@Userid)";
            if (sc.State != ConnectionState.Open)
            {
                MessageBox.Show("注册时，数据库连接失败！");
                return;
            }
            using (SqlCommand command = new SqlCommand(sql, sc))
            {
                command.Parameters.AddWithValue("@Begintime", begintime);
                command.Parameters.AddWithValue("@Endtime", endtime);
                command.Parameters.AddWithValue("@Workid", workid);
                command.Parameters.AddWithValue("@Userid", userid);
                command.ExecuteNonQuery();
                sc.Close();
                return;
            }
        }


        //根据签到id删除签到信息
        public void DeleteSignsById(int id)
        {
            // 在这里执行具体的数据库操作
            // 例如使用 SqlCommand 执行查询语句或执行存储过程
            SqlConnection sc = new SqlConnection(connectionString);
            sc.Open();
            String sql = "delete from signs where signid=@SignId";
            if (sc.State != ConnectionState.Open)
            {
                MessageBox.Show("删除时，数据库连接失败！");
                return;
            }
            using (SqlCommand command = new SqlCommand(sql, sc))
            {
                command.Parameters.AddWithValue("@SignId", id);
                command.ExecuteNonQuery();
                sc.Close();
                return;
            }
        }

        //根据用户id删除签到信息
        public void DeleteSignsByUserId(int id)
        {
            // 在这里执行具体的数据库操作
            // 例如使用 SqlCommand 执行查询语句或执行存储过程
            SqlConnection sc = new SqlConnection(connectionString);
            sc.Open();
            String sql = "delete from signs where userid=@UserId";
            if (sc.State != ConnectionState.Open)
            {
                MessageBox.Show("删除时，数据库连接失败！");
                return;
            }
            using (SqlCommand command = new SqlCommand(sql, sc))
            {
                command.Parameters.AddWithValue("@UserId", id);
                command.ExecuteNonQuery();
                sc.Close();
                return;
            }
        }

        //根据签到id更新签到信息
        public void UpdateSignsById(int id, DateTime begintime, DateTime endtime)
        {
            // 在这里执行具体的数据库操作
            // 例如使用 SqlCommand 执行查询语句或执行存储过程
            SqlConnection sc = new SqlConnection(connectionString);
            sc.Open();
            String sql = "update signs set begintime=@Begintime,endtime=@Endtime where signid=@SignId";
            if (sc.State != ConnectionState.Open)
            {
                MessageBox.Show("更新时，数据库连接失败！");
                return;
            }
            using (SqlCommand command = new SqlCommand(sql, sc))
            {
                command.Parameters.AddWithValue("@Begintime", begintime);
                command.Parameters.AddWithValue("@Endtime", endtime);
                command.Parameters.AddWithValue("@SignId", id);
                command.ExecuteNonQuery();//这句话在更新语句中需要存在才能执行更新语句
                sc.Close();
                return;
            }
        }

        //根据用户id获取用户全部人员签到信息
        public List<Signs> GetSignsByUserId(int id)
        {
            SqlConnection sc = new SqlConnection(connectionString);
            sc.Open();
            String sql = "select * from signs where userid = @Userid";
            if (sc.State != ConnectionState.Open)
            {
                MessageBox.Show("打开时，数据库连接失败！");
                return null;
            }
            using (SqlCommand command = new SqlCommand(sql, sc))
            {
                command.Parameters.AddWithValue("@Userid", id);
                using (SqlDataReader reader = command.ExecuteReader())
                {
                    List<Signs> signs = new List<Signs>();
                    while (reader.Read())
                    {
                        // 处理查询结果
                        // 例如输出数据到控制台或存储到集合中
                        int outid = reader.GetInt32(0);
                        DateTime begintime = reader.GetDateTime(1);
                        DateTime endtime = reader.GetDateTime(2);
                        int workid = reader.GetInt32(3);
                        int userid = reader.GetInt32(4);
                        Signs f = new Signs(outid, begintime, endtime , workid, userid);
                        signs.Add(f);
                    }
                    sc.Close();
                    return signs;
                }
            }
        }
        //


        //根据签到id获取用户全部人员签到信息
        public List<Signs> GetSignsById(int id)
        {
            SqlConnection sc = new SqlConnection(connectionString);
            sc.Open();
            String sql = "select * from signs where signid = @Signid";
            if (sc.State != ConnectionState.Open)
            {
                MessageBox.Show("打开时，数据库连接失败！");
                return null;
            }
            using (SqlCommand command = new SqlCommand(sql, sc))
            {
                command.Parameters.AddWithValue("@Signid", id);
                using (SqlDataReader reader = command.ExecuteReader())
                {
                    List<Signs> signs = new List<Signs>();
                    while (reader.Read())
                    {
                        // 处理查询结果
                        // 例如输出数据到控制台或存储到集合中
                        int outid = reader.GetInt32(0);
                        DateTime begintime = reader.GetDateTime(1);
                        DateTime endtime = reader.GetDateTime(2);
                        int workid = reader.GetInt32(3);
                        int userid = reader.GetInt32(4);
                        Signs f = new Signs(outid, begintime, endtime, workid, userid);
                        signs.Add(f);
                    }
                    sc.Close();
                    return signs;
                }
            }
        }
        //


        //根据签到开始时间获取用户全部人员签到信息
        public List<Signs> GetSignsByTime(DateTime _begintime)
        {
            SqlConnection sc = new SqlConnection(connectionString);
            sc.Open();
            String sql = "select * from signs where begintime between @BeginTime and @MoBeginTime";
            if (sc.State != ConnectionState.Open)
            {
                MessageBox.Show("打开时，数据库连接失败！");
                return null;
            }
            using (SqlCommand command = new SqlCommand(sql, sc))
            {
                command.Parameters.AddWithValue("@BeginTime", _begintime);
                command.Parameters.AddWithValue("@MoBeginTime", _begintime.AddDays(1));
                using (SqlDataReader reader = command.ExecuteReader())
                {
                    List<Signs> signs = new List<Signs>();
                    while (reader.Read())
                    {
                        // 处理查询结果
                        // 例如输出数据到控制台或存储到集合中
                        int outid = reader.GetInt32(0);
                        DateTime begintime = reader.GetDateTime(1);
                        DateTime endtime = reader.GetDateTime(2);
                        int workid = reader.GetInt32(3);
                        int userid = reader.GetInt32(4);
                        Signs f = new Signs(outid, begintime, endtime,workid,userid);
                        signs.Add(f);
                    }
                    sc.Close();
                    return signs;
                }
            }
        }
        //


        //获取用户全部人员出差信息
        public List<Signs> GetSignss()
        {
            SqlConnection sc = new SqlConnection(connectionString);
            sc.Open();
            String sql = "select * from signs";
            if (sc.State != ConnectionState.Open)
            {
                MessageBox.Show("打开时，数据库连接失败！");
                return null;
            }
            using (SqlCommand command = new SqlCommand(sql, sc))
            {
                //command.Parameters.AddWithValue("@Place", _place);
                using (SqlDataReader reader = command.ExecuteReader())
                {
                    List<Signs> sings = new List<Signs>();
                    while (reader.Read())
                    {
                        // 处理查询结果
                        // 例如输出数据到控制台或存储到集合中
                        int outid = reader.GetInt32(0);
                        DateTime begintime = reader.GetDateTime(1);
                        DateTime endtime = reader.GetDateTime(2);
                        int workid = reader.GetInt32(3);
                        int userid = reader.GetInt32(4);
                        Signs f = new Signs(outid, begintime, endtime, workid, userid);
                        sings.Add(f);
                    }
                    sc.Close();
                    return sings;
                }
            }
        }
        //


    }
}
